<?php 
// error_reporting(0);
// header('Content-type:text/html; charset=utf-8');
require './dnspod/dnspod.php';
require './dnspod/likexian.php';
$domains = include_once('./domain.php');

$dnspod = new dnspod();
@session_start();
$_POST['login_email'] = $domains['email'];
$_POST['login_password'] = $domains['passwd'];
$_GET['action'] = 'domainlist';
$domains = array_slice($domains, 2);

if ($_GET['action'] == 'domainlist') {
	if ($_POST['login_email'] == '') {
		if ($_SESSION['login_email'] == '') {
			exit('请输入登录账号。');
		} 
	} else {
		$_SESSION['login_email'] = $_POST['login_email'];
	} 

	if ($_POST['login_password'] == '') {
		if ($_SESSION['login_password'] == '') {
			exit('请输入登录密码。');
		} 
	} else {
		$_SESSION['login_password'] = $_POST['login_password'];
	} 

	$response = $dnspod -> api_call('Domain.List', array());
	$domain_ids = array();
	foreach ($response['domains'] as $id => $domain) {
		$domain_ids[$domain['name']] = $domain['id'];
		$list .= "<tr><td>{$domain['id']}</td><td>{$domain['name']}</td><td>{$domain['grade']}</td><td>{$domain['status']}</td><td>{$domain['ext_status']}</td><td>{$domain['records']}</td><td>{$domain['is_mark']}</td><td>{$domain['updated_on']}</td><td><a href='?action=recordlist&domain_id={$domain['id']}'>记录</a> <a href='?action=domainremove&domain_id={$domain['id']}'>删除</a></td></tr>";
	} 
//	echo '<pre>';
	foreach($domains as $t_domain => $domain_ip) {
		//echo $t_domain;
		$domain_id = $domain_ids[$t_domain];
		$response = $dnspod -> api_call('Record.List', array('domain_id' => $domain_id));
		$records = array();
		$recordids = array();
		//print_r($response['records']);
		foreach ($response['records'] as $id => $record) {
			if ($record['type'] == 'A') {
				$records[$record['name']] = $record['value'];
				$recordids[$record['name']] = $record['id'];
				//echo "\r" . $recordids[$record['name']] . ' ' . $record['value'] . "\n";
			} 
		} 
	//	print_r($records);
		//print_r($recordids);
		//die();
		// www
		if (isset($records['www'])) {
			if ($records['www'] != $domain_ip) {
				// 跟新记录
				$record_id = $recordids['www'];
				$res = $dnspod -> api_call('Record.Modify', array('domain_id' => $domain_id, 'record_id' => $record_id, 'sub_domain' => 'www', 'record_type' => 'A', 'record_line' => '默认', 'value' => $domain_ip));
			} 
		} else {
			$res = $dnspod -> api_call('Record.Create', array('domain_id' => $domain_id, 'sub_domain' => 'www', 'record_type' => 'A', 'record_line' => '默认', 'value' => $domain_ip));
		} 
		echo $t_domain . ' www  ' . $domain_ip . "\n"; 
		// @
		if (isset($records['@'])) {
			if ($records['@'] != $domain_ip) {
				// 跟新记录
				$record_id = $recordids['@'];
				$res = $dnspod -> api_call('Record.Modify', array('domain_id' => $domain_id, 'record_id' => $record_id, 'sub_domain' => '@', 'record_type' => 'A', 'record_line' => '默认', 'value' => $domain_ip));
			} 
		} else {
			$res = $dnspod -> api_call('Record.Create', array('domain_id' => $domain_id, 'sub_domain' => '@', 'record_type' => 'A', 'record_line' => '默认', 'value' => $domain_ip));
		} 
		echo $t_domain . ' @  ' . $domain_ip . "\n"; 
		// *
		if (isset($records['*'])) {
			if ($records['*'] != $domain_ip) {
				// 跟新记录
				$record_id = $recordids['*'];
				$res = $dnspod -> api_call('Record.Modify', array('domain_id' => $domain_id, 'record_id' => $record_id, 'sub_domain' => '*', 'record_type' => 'A', 'record_line' => '默认', 'value' => $domain_ip));
			} 
		} else {
			$res = $dnspod -> api_call('Record.Create', array('domain_id' => $domain_id, 'sub_domain' => '*', 'record_type' => 'A', 'record_line' => '默认', 'value' => $domain_ip));
		} 
		echo $t_domain . ' *  ' . $domain_ip . "\n";
		//die();
		sleep(rand(3,10));
	} 

	die();

	echo str_replace('{domain_list}', $list, $domain_list);
} elseif ($_GET['action'] == 'domaincreate') {
	if ($_POST['domain'] == '') {
		exit('参数错误。');
	} 

	$response = $dnspod -> api_call('Domain.Create', array('domain' => $_POST['domain']));

	exit('添加成功，<a href="?action=domainlist">点击返回</a>。');
} elseif ($_GET['action'] == 'domainremove') {
	if ($_GET['domain_id'] == '') {
		exit('参数错误。');
	} 

	$response = $dnspod -> api_call('Domain.Remove', array('domain_id' => $_GET['domain_id']));

	exit('删除成功，<a href="?action=domainlist">点击返回</a>。');
} elseif ($_GET['action'] == 'recordlist') {
	if ($_GET['domain_id'] == '') {
		exit('参数错误。');
	} 

	$response = $dnspod -> api_call('Record.List', array('domain_id' => $_GET['domain_id']));

	foreach ($response['records'] as $id => $record) {
		$list .= "<tr><td>{$record['id']}</td><td>{$record['name']}</td><td>{$record['type']}</td><td>{$record['line']}</td><td>{$record['value']}</td><td>{$record['enabled']}</td><td>{$record['mx']}</td><td>{$record['ttl']}</td><td></td></tr>";
	} 

	echo str_replace('{record_list}', $list, $record_list);
} else {
	echo $login_form;
} 

